package com.msos.servlet.phoneAdd;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.msos.model.customer.Customer;
import com.msos.model.respBean.RespBean;
import com.msos.service.phoneAddService.phoneAddService;
import com.msos.utils.CommonsUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/phoneAdd")
public class phoAddServlet extends HttpServlet {
      phoneAddService phoneaddService=new phoneAddService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String customerName=request.getParameter("customerName");
        String gender=request.getParameter("gender");
        String idCard=request.getParameter("idCard");
        String phone=request.getParameter("phone");
        //String aginPhone=request.getParameter("aginPhone");
        String password=request.getParameter("password");
        String cardType=request.getParameter("cardType");
        Customer customer=new Customer();
        customer.setUsername(customerName);  customer.setGender(gender);  customer.setIdCard(idCard);
        customer.setPhone(phone);  customer.setPassword(password);  customer.setCardType(cardType);
        customer.setMid(22); customer.setEnabled(true);//设置操作员 账户状态
        Integer r1=phoneaddService.phoneAdd(customer);
        RespBean respBean=null;
        if(r1== CommonsUtils.SUCCESS){
            respBean=RespBean.ok("开户成功");
        }else if (r1==CommonsUtils.FAIL){
            respBean=RespBean.error("开户失败");
        }else{
            respBean=RespBean.error("开户x异常");
        }
        response.setContentType("application/json;charset=utf-8");
        ObjectMapper om=new ObjectMapper();
        PrintWriter out = response.getWriter();
        String json= om.writeValueAsString(respBean);
        out.write(json);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");//
        if("page".equals(action)){
            request.getRequestDispatcher("WEB-INF/jsp/phoneAdd/phoneAdd.jsp").forward(request,response);
        }
    }
}
